package com.woniuxy.AOP;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @ClassName MyAspect
 * @Description TODO
 * @Author LI.hh
 * @Date 2022/8/22 19:33
 * @Version 1.0
 */
@Component
@Aspect
public class MyAspect implements MethodInterceptor {
    @Pointcut("execution(* com.woniuxy.service.impl.*.*(..))")
    private void myPointCut(){}
    //定义通知: 前置通知
    @Before("myPointCut()")
    public void checkPermission(){
        System.out.println("切入了方法");
    }
    //定义后置通知
    @AfterReturning("myPointCut()")
    public void commitTransaction(){
        System.out.println("提交了方法");
    }

    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知开启：");
        return null;
    }

    public void myAfterThrowing(JoinPoint joinPoint, Throwable e){
        System.out.println("抛出异常："+e.getMessage());
    }

    public void myAfter(){
        System.out.println("最终通知的执行！");
    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        checkPermission();
        if(invocation.getMethod().getName().startsWith("select")){
            System.out.println("是查询方法");
        }else if(invocation.getMethod().getName().startsWith("add")){
            System.out.println("是添加方法");
        }else if(invocation.getMethod().getName().startsWith("update")){
            System.out.println("是修改方法");
        }else if(invocation.getMethod().getName().startsWith("delete")){
            System.out.println("是删除方法");
        }
        commitTransaction();
        Object proceed = invocation.proceed();
        return proceed;
    }
}
